<%@ page language="java" contentType="text/html; charset=UTF-8" isELIgnored="false"
    pageEncoding="UTF-8"%>
<%@taglib uri="/struts-tags" prefix="s"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>源Ip网站配置</title>
<%@ include file="/pages/common/header.jsp" %>

<script type="text/javascript">
var isEdit = false;
var editUrlDiv;
//表格列数据
var columns=[
	{display:"序号",width:50,render:function(rowData,rowIndex){
		return (parseInt(this.get("page"))-1)*parseInt(this.get("pageSize"))+rowIndex+1;
	}},
     { display: "id", name: "id"}, 
	 { display: "网站标识", name:"code",width:80},   
     { display: "创建时间", name: "createTime",width:100},   
     { display: "字符集编码", name: "encoding",width:60},   
     { display: "最后修改时间", name: "lastTime",width:100},   
     { display: "网站名称", name: "name",width:80},   
     { display: "单行解释器", name: "rowRegex",width:130,},   
     { display: "抓取间隔时间", name: "splitTime",width:80,type:'int',
    	                render:function(item){
    	                	return item.splitTime/1000+'秒';
    	                }
     },     
     { display: "下载解析地址", name: "urlRegex",width:130},   
     { display: "创建人", name: "userName",width:60},
     { display: "orderid", name: "orderid",width:60},
     { display: "是否有效", name: "state", type:'int',
    	                 render:function(item){
    	                	 switch(parseInt(item.state)){case 0:return '无效';case 1:return '有效';}
    	                 }
     }
];

var toolbarOptions = {
		items : [ {
			id : "add",
			text : '增加',
			click : toolbarBtnItemClick,
			img : "../../images/icons/toolbar/add.png"
		}, {
			line : true
		}, {
			id : "modify",
			text : '修改',
			click : toolbarBtnItemClick,
			img : "../../images/icons/toolbar/page_edit.gif"
		}, {
			line : true
		}, {
			id : "remove",
			text : '删除',
			click : toolbarBtnItemClick,
			img : "../../images/icons/toolbar/page_delete.gif"
		} ]
	};
$(function(){
	initGrid();
});

function initGrid(){
	var url="<%=request.getContextPath()%>/operate/ipWebsiteManager/search.action";
	var g = $("#ressult_div").ligerGrid({
		height:'100%',
		width:'100%',
		columns:columns,
		enabledSort:false,
		pageSize:50,
		toolbar : toolbarOptions,
		root:"result",//数据集合
		record:"dataSize",//总数据数
		pageParmName:"pageIndex",//页索引
		pagesizeParmName:"pageLen",//页长
		usePager:true,
		checkbox : true,
		dataAction:"local",
		method:"post",
		delayLoad:false,//初始化不加载
		pageSizeOptions:[10,20,50,100],
		url:url
	});
	var manager = $("#ressult_div").ligerGetGridManager(); 
	manager.set("height","100%");
    g.toggleCol("id",false);
    return g;
}

//工具条事件
function toolbarBtnItemClick(item) {
	switch (item.id) {
	case "add":
	    isEdit = false;
		editUrlDiv = $.ligerDialog.open({
			title : "增加网站",
			isResize : true,
			width : 430,
			height : 350,
			isHidden : true,
			buttons : [
		   {
				id : 'Validate',
				text : '验证',
				onclick : check
			},{
				id : 'save',
				text : '保存',
				onclick : btnClick
			}, {
				id : 'cancel',
				text : '取消',
				onclick : btnClick
			} ],
			target : $("#editUrl")
		});
		break;
	case "modify":
		var manager=$("#ressult_div").ligerGetGridManager();
		var rows = manager.getCheckedRows();
		if (rows.length > 1) {
			$.ligerDialog.alert('只能选中一行!');
			return;
		}
		if (rows.length == 0) {
			$.ligerDialog.alert('请选择行!');
			return;
		}
		selectedRow = rows[0];
		isEdit = true;
		$("#code").val(manager.getSelected().code);
		$("#encoding").val(manager.getSelected().encoding);
		$("#name").val(manager.getSelected().name);
		$("#rowRegex").val(manager.getSelected().rowRegex);
		$("#splitTime").val(manager.getSelected().splitTime);
		$("#urlRegex").val(manager.getSelected().urlRegex);
		$("#state").val(manager.getSelected().state);
		$("#orderid").val(manager.getSelected().orderid);
		editUrlDiv = $.ligerDialog.open({
			title : "修改网站",
			isResize : true,
			width : 430,
			height : 350,
			isHidden : true,
			buttons : [
			            {
				id : 'Validate',
				text : '验证',
				onclick : check
			},{
				id : 'save',
				text : '保存',
				onclick : btnClick
			}, {
				id : 'cancel',
				text : '取消',
				onclick : btnClick
			} ],
			target : $("#editUrl")
		});
		break;
	case "remove":
		var manager = $("#ressult_div").ligerGetGridManager();
		selectedRow = manager.getSelectedRow();
		if (!selectedRow) {
			$.ligerDialog.alert('请选择行!');
			return;
		}
		jQuery.ligerDialog.confirm('确定删除吗?', function(confirm) {
			if (confirm) {
				f_delete();
			}
		});
		break;
	}
}

function btnClick(button) {
	if (button.id == "save") {
	    saveUrlResource();	
	} else if (button.id == "cancel") {
		editUrlDiv.hidden();
	}
}

function saveUrlResource(){

	var code=$("#code").val();
    var encoding=$("#encoding").val();
    var name=$("#name").val();
    var rowRegex=$("#rowRegex").val();
    var splitTime=$("#splitTime").val();
    var urlRegex=$("#urlRegex").val();
    var state=$("#state").val();
    var orderid=$("#orderid").val();
	//判断
    if(isEdit)
    {
	    	var id=$("#ressult_div").ligerGetGridManager().getSelected().id;
		    $.ajax({
		    	type:"POST",
		    	url:"<%=request.getContextPath()%>/operate/ipWebsiteManager/updateIpWebsite.action",
		    	dataType:'json',
		    	data:{
		    		"vo.id":id,
		    		"vo.code":code,
		    		"vo.encoding":encoding,
		    		"vo.name":name,
		    		"vo.rowRegex":rowRegex,
		    		"vo.splitTime":splitTime,
		    		"vo.urlRegex":urlRegex,
		    		"vo.state":state,
		    		"vo.orderid":orderid
		    	},
		    	loading : "正在保存中...",
		    	success:function(data){
			    		        	$.ligerDialog.success("保存成功", '提示信息');
			    		        	$("#ressult_div").ligerGetGridManager().loadData();

			},
		 	error:function(){
		 		alert("服务器异常，请稍后再试！");
		 		isSuccess = false;
		 	}	
		    });
		    editUrlDiv.hidden();
    	
        }else
      	  {
       	  $.ajax({
		    	type:"POST",
		    	url:"<%=request.getContextPath()%>/operate/ipWebsiteManager/addIpWebsite.action",
		    	dataType:'json',
		    	data:{
		    		"vo.code":code,
		    		"vo.encoding":encoding,
		    		"vo.name":name,
		    		"vo.rowRegex":rowRegex,
		    		"vo.splitTime":splitTime,
		    		"vo.urlRegex":urlRegex,
		    		"vo.state":state
		    	},
		    	loading : "正在保存中...",
		    	success:function(data){
		    		        	$.ligerDialog.success("保存成功", '提示信息');
		    		        	$("#ressult_div").ligerGetGridManager().loadData();	
			},
		 	error:function(){
		 		alert("服务器异常，请稍后再试！");
		 		isSuccess = false;
		 	}	
		    });
       	  editUrlDiv.hidden();
      	  }
	
}

function check(){
	var code=$("#code").val();
    var encoding=$("#encoding").val();
    var name=$("#name").val();
    var rowRegex=$("#rowRegex").val();
    var splitTime=$("#splitTime").val();
    var urlRegex=$("#urlRegex").val();
    var state=$("#state").val();
    var orderid=$("#orderid").val();
    //输入框验证
					    if(""==code) {
					    	     $("#code").ligerTip({content:"字段不能为空",
					    	    		              callback:function()
									                         {
									                             setTimeout(function ()
									
									                             {
									
									                                 $("#code").ligerHideTip(); //5秒延迟后关闭tip
									
									                             }, 5000);
									                         }
					
					                     }) ;return;
					    	}
					    
					    if(""==name) {
					    	     $("#name").ligerTip({content:"字段不能为空",
											    	    	 callback:function()
										                     {
										                         setTimeout(function ()
										
										                         {
										
										                             $("#name").ligerHideTip(); //5秒延迟后关闭tip
										
										                         }, 5000);
										                     }
					    	    	   }) ;return;
					    	}

					    if(""==rowRegex) {
					    	     $("#rowRegex").ligerTip({content:"字段不能为空",
											    	    	 callback:function()
										                     {
										                         setTimeout(function ()
										
										                         {
										
										                             $("#rowRegex").ligerHideTip(); //5秒延迟后关闭tip
										
										                         }, 5000);
										                     }
					    	    	   }) ;return;
					    	}
					    if(""==splitTime) {
					    	     $("#splitTime").ligerTip({content:"字段不能为空",
											    	    	 callback:function()
										                     {
										                         setTimeout(function ()
										
										                         {
										
										                             $("#splitTime").ligerHideTip(); //5秒延迟后关闭tip
										
										                         }, 5000);
										                     }
					    	    	   }) ;return;
					    	}
					    if(""==urlRegex) {
						     $("#urlRegex").ligerTip({content:"字段不能为空",
										    	    	 callback:function()
									                     {
									                         setTimeout(function ()
									
									                         {
									
									                             $("#urlRegex").ligerHideTip(); //5秒延迟后关闭tip
									
									                         }, 5000);
									                     }
						    	   }) ;return;
						}
					    if(""==state) {
						     $("#state").ligerTip({content:"字段不能为空",
										    	    	 callback:function()
									                     {
									                         setTimeout(function ()
									
									                         {
									
									                             $("#state").ligerHideTip(); //5秒延迟后关闭tip
									
									                         }, 5000);
									                     }
						    	   }) ;return;
						}
    $.ajax({
    	type:"POST",
    	url:"<%=request.getContextPath()%>/operate/ipWebsiteManager/checkWebsite.action",
    	dataType:'json',
    	data:{
    		"vo.code":code,
    		"vo.encoding":encoding,
    		"vo.name":name,
    		"vo.rowRegex":rowRegex,
    		"vo.splitTime":splitTime,
    		"vo.urlRegex":urlRegex,
    		"vo.state":state,
    		"vo.orderid":orderid
    	},
    	loading : "正在保存中...",
    	success:function(res){
    		            if(res.length>0)
    		            	{
    		            	   var message="";
    		            	   for(var i=0;i<res.length;i++){
       		            	       message+=res[i];
    		            	   } 
	    		            	$.ligerDialog.success(message, '提示信息');
    		            	}
    		            
    		        	
	},
 	error:function(){
 		alert("服务器异常，请稍后再试！");
 		isSuccess = false;
 	}	
    });
     //editUrlDiv.hidden();
}
function f_delete(){
	var manager = $("#ressult_div").ligerGetGridManager();
	if (manager.getCheckedRows().length == 0) {
		$.ligerDialog.alert('请选择行!');
		return;
	}
	
	if (manager.getCheckedRows().length > 1) {
		$.ligerDialog.alert('只能选中一行!');
		return;
	}
	 var id =manager.getSelected().id;

	 var url="<%=request.getContextPath()%>/operate/ipWebsiteManager/deleteWebsite.action";
	$.ajax({
    	type:"POST",
    	url:url,
    	data:{
    		"vo.id":id
    	},
    	loading : "正在删除中...",
    	success:function(data){
    		$.ligerDialog.success("操作成功", '提示信息');
    		$("#ressult_div").ligerGetGridManager().loadData();
		},
	 	error:function(){
	 		alert("服务器异常，请稍后再试！");
	 		isSuccess = false;
	 	}	
    });
}

</script>
</head>
<body>

<div class="main"  style="height:100%">
	<div style="height:25px;border:0px;"></div>
	<div id="ressult_div" style="height:100%"><span/></div>
	
	 <div id="editUrl" style="display: none;">
	  <form id="ipwebsite_form">
	  	<table class="form2column">
	  		
	  		<tr>
	  			<td class="label">网站标识</td>
	  			<td class="content"><input type="text" id="code" name="code_name"  /></td>
	  		</tr>
	  		
	  		<tr>
	  			<td class="label">字符集编码</td>
	  			 <td class="content"><input type="text" id="encoding" name="encoding_name"  /></td>
	  			<!-- 
	  			<td class="content">
	  			<select style="width:155px;height:25px" id="encoding" name="encoding_name" >
	  					<option value="">--请选择--</option>
	  					<option value="ASCII">ASCII</option>
	  					<option value="GB2312">GB2312</option>
	  					<option value="BIG5">BIG5</option>
	  					<option value="GBK">GBK</option>
	  					<option value="GB18030">GB18030</option>
	  					<option value="UCS">UCS</option>
	  					<option value="UNICODE">UNICODE</option>
	  					<option value="UTF-8">UTF-8</option>
	  					<option value="UTF-16">UTF-16</option>
	  					<option value="UTF-32">UTF-32</option>
	  					<option value="ISO-8859-7">ISO-8859-7</option>
	  			</select>
	  			</td>
	  			 -->
	  		</tr>
	  		<tr>
	  			<td class="label">网站名称</td>
	  			<td class="content"><input type="text" id="name" name="name_name" /></td> 
	  		</tr>
	  		<tr>
	  			<td class="label">单行解释器</td>
	  			<td class="content"><input type="text" id="rowRegex" name="rowRegex_name" /></td>
	  		</tr>
	  		<tr>
	  			<td class="label">抓取时间间隔</td>
	  			<td class="content"><input type="text" id="splitTime" name="splitTime_name" /></td>
	  		</tr>
	  		<tr>
	  			<td class="label">下载解析地址</td>
	  			<td class="content"><input type="text" id="urlRegex" name="urlRegex_name"  /></td>
	  		</tr>
	  		<tr>
	  			<td class="label">orderid</td>
	  			<td class="content"><input type="text" id="orderid" name="orderid_name"  /></td>
	  		</tr>
	  		<tr>
	  			<td class="label">是否有效</td>
	  			<td class="content">
	  				<select style="width:155px;height:25px" id="state" name="state_name"  >
	  					<option value="">--请选择--</option>
	  					<option value="0">无效</option>
	  					<option value="1">有效</option>
	  				</select>
	  			</td>
	  		</tr>
	  	</table>
	  </form>
  </div>
</div>
</body>
</html>